﻿using System;

namespace MLSharp.Filters
{
	/// <summary>
	/// Filters a data set down to a smaller subsample of
	/// the original.
	/// </summary>
	public class SubsampleFilter : IDataSetFilter
	{
		#region Public Properties

		/// <summary>
		/// The subsample factor.
		/// </summary>
		public double SubsampleSize { get; set; }

		#endregion

		#region Public Constructors

		/// <summary>
		/// Creates an uninitialized filter.
		/// </summary>
		public SubsampleFilter()
		{
			
		}

		/// <summary>
		/// Creates a filter that will downsample data sets to the
		/// specified relative size.  
		/// </summary>
		/// <param name="size">The relative size factor in the (0,1) range.</param>
		public SubsampleFilter(double size)
		{
			if (size <= 0 || size >= 1)
			{
				throw new ArgumentOutOfRangeException("size", size, "Must be greater than 0 and less than 1.");
			}

			SubsampleSize = size;
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Creates a subsample from the specified data set.
		/// </summary>
		/// <param name="dataSet"></param>
		public void Transform(IDataSet dataSet)
		{
			IDataSet ds = Sample.CreateSubsample(dataSet, SubsampleSize);

			dataSet.Instances.Clear();
			ds.MoveInstancesTo(dataSet);
		}

		#endregion
	}
}
